﻿package dt
{	
	
	import dt.exchangeddata.BetInfo;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import game.BetController;
	import game.BetPlace;
	import game.events.EventData;
	import common.Utility;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class BetController extends game.BetController {						
		public var dragonHit:SimpleButton;
		public var tigerHit:SimpleButton;
		public var tieHit:SimpleButton;		
		
		public var dragonBet:BetPlace;
		public var tigerBet:BetPlace;
		public var tieBet:BetPlace;
		
		public var betInfo:BetInfo;
		public var saveBetInfo:BetInfo;
		public var preBetInfo:BetInfo;		
		
		public function BetController() {
			super.rebetBtn.visible = false;
			super.txtRebetTf.visible = false;
			
			this.betInfo = new BetInfo();
			this.saveBetInfo = new BetInfo();
			this.preBetInfo = new BetInfo();			
			this.hitArr = [this.dragonHit, this.tigerHit, this.tieHit];
		}
		
		/**
		 * @author Daisy
		 * @param	minBet
		 * @param	maxBet
		 * @param	currentBalance
		 * @param	initChipValueArr
		 */
		 override public function initBet(minBet:Number, maxBet:Number, currentBalance:Number, initChipValueArr:Array):void {			
			super.initBet(minBet, maxBet, currentBalance, initChipValueArr);
			
			var newBetArr:Array = super.initBetPlace(super.hitArr);						
			
			this.dragonBet = newBetArr[0];
			this.tigerBet = newBetArr[1];
			this.tieBet = newBetArr[2];
			this.betArr = [this.dragonBet, this.tigerBet, this.tieBet];
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		override public function confirmBtnClickHandler(e:MouseEvent):void {
			dispatchEvent(new EventData(EventData.CONFIRM_BETTING, true));		
		}	
		
		/**
		 * @author Daisy 
		 */
		public function saveBetting():void {
			for (var i:int = 0; i < this.betInfo.arrBettingValue.length; i++ ) {
				this.betInfo.arrBettingValue[i] = BetPlace(this.betArr[i]).getBetValue();				
			}
		}
		/**
		 * @author Daisy
		 * @param	e
		 */
		override public function clearBtnClickHandler(e:MouseEvent):void {
			var tmpBetting:Number = 0;
			tmpBetting = this.getTotalBetValue() - this.saveBetInfo.getTotalBet();			
			if (tmpBetting > 0) {						
				currentBalance = Utility.fixNumber(currentBalance + tmpBetting);
				var event:EventData = new EventData(EventData.CLEAR_BETTING, true);				
				event.newBalance = currentBalance;	
				dispatchEvent(event);				
			}		
		}
		
		/**
		 * @author Daisy
		 * @param	e		 
		 */
		override public function rebetBtnClickHandler(e:MouseEvent):void {		
			if (this.preBetInfo.getTotalBet() > 0) {
				//set currentBalance clear before rebet
				var tmpBetting:Number = 0;
				tmpBetting = this.getTotalBetValue() - this.saveBetInfo.getTotalBet();
				currentBalance = Utility.fixNumber(currentBalance + tmpBetting);
				//set currentBalance after rebet
				currentBalance = Utility.fixNumber(currentBalance - this.preBetInfo.getTotalBet());
				var event:EventData = new EventData(EventData.REBET_BETTING, true);
				event.newBalance = currentBalance;	
				dispatchEvent(event);				
			}			
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 * @usage update value betting		 
		 */
		override public function updateBet(info:Object):void {
			this.betInfo = BetInfo(info);	
			for (var i:int = 0; i < this.betInfo.arrBettingCode.length; i++) {
				BetPlace(this.betArr[i]).setBetValue(this.betInfo.arrBettingValue[i]);
			}
		}
	}
}